package com.woniuxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.daos.TbBedDao;
import com.woniuxy.daos.TbDepartmentDao;
import com.woniuxy.daos.TbHospitalManagerDao;
import com.woniuxy.daos.TbHospitalDao;
import com.woniuxy.entitys.TbBed;
import com.woniuxy.entitys.TbDepartment;
import com.woniuxy.entitys.TbHospitalManager;
import com.woniuxy.entitys.TbHospital;
import com.woniuxy.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private TbDepartmentDao tbDepartmentDao;
    @Autowired
    private TbHospitalManagerDao hospitalManagerDao;
    @Autowired
    private TbHospitalDao tbHospitalDao;
    @Autowired
    private TbBedDao tbBedDao;
    @Override
    public List<TbHospital> getHospitals() {
        //查找所有的医院
        List<TbHospital> list = tbHospitalDao.getHospitals();
        //查找所有的科室
        List<TbDepartment> departments = tbDepartmentDao.getDepartments();
        //查找所有的床位
        List<TbBed> beds = tbBedDao.getBed();
        for (TbHospital hospital : list) {
            for (TbDepartment department : departments) {
                if(department.getHid()==hospital.getId()){
                    hospital.getDepartments().add(department);
                }
            }
        }
        return list;
    }


    @Override
    public List<TbDepartment> getDepartments() {
        //查找所有的科室
        List<TbDepartment> list = tbDepartmentDao.getDepartments();
        return list;
    }

    @Override
    public PageInfo<TbDepartment> getDepartmentByHid(Integer hid, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<TbDepartment> list = tbDepartmentDao.getDepartmentByHid(hid);
        PageInfo<TbDepartment> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
    @Override
    public List<TbDepartment> getAllDepartment() {
        List<TbDepartment> allDepartment = tbDepartmentDao.getAllDepartment();
        return allDepartment;
    }

    @Override
    public List<TbDepartment> selDepartmentsByUid(Integer uid) {
        return tbDepartmentDao.selDepartmentByLoginuser(uid);

    }

    @Override
    public int softdelDep(Integer did) {
        int i = tbDepartmentDao.updateSoftdel(did);
        return i;
    }

    @Override
    public List<TbDepartment> selByCondition(Integer uid,String keyword) {
        List<TbDepartment> list = tbDepartmentDao.selDepartmentByCondition(uid,keyword);
        return list;
    }

    @Override
    public int saveAdd(String dname, Integer uid) {
        List<TbDepartment> allDepartment = tbDepartmentDao.getAllDepartment();
        boolean tag = true;
        for(TbDepartment department: allDepartment){
            if(department.getDname().contains(dname)){
                tag=false;
                break;
            }
        }
        TbDepartment department = new TbDepartment();
        department.setDname(dname);
        department.setSoftdel(1);
        Integer hid = hospitalManagerDao.selectHid(uid);
        department.setHid(hid);
        System.out.println(department+"00000000000000");
        int i = tbDepartmentDao.insert(department);
        return i;
    }


}
